package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	hello "grpc/proto"
	"log"
	"net"
)

const (
	Address = "127.0.0.1:1234"
)

type helloService struct {}

var HelloService = helloService{}

func (hs helloService) SayHello(ctx context.Context, in *hello.HelloRequest) (*hello.HelloResponse, error) {
	resp := new(hello.HelloResponse)
	resp.Message = fmt.Sprintf("Hello %s.", in.Name)
	return resp, nil
}

func (hs helloService) SayHi(ctx context.Context, in *hello.HiRequest) (*hello.HiResponse, error) {
	resp := new(hello.HiResponse)
	resp.Message = fmt.Sprintf("Hi %s, grade=%d, school=%s, grde=%d status=%d", in.Name, in.Grade, in.School, in.Grade, in.Status)
	return resp, nil
}

func main() {
	listen, err := net.Listen("tcp", Address)
	if err != nil {
		log.Panicf("failed to listen: %v", err)
	}

	s := grpc.NewServer()
	hello.RegisterHelloServer(s, HelloService)
	fmt.Println("Listen on " + Address)
	s.Serve(listen)
}
